package com.sun.zxing.util;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author Administrator
 */
public class ZXingUtil {

    public static void encodeImg(String imgPath,String format,String content,int width, int height,String logo) throws Exception {

        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        //排错率  L<M<Q<H
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //外边距：margin
        hints.put(EncodeHintType.MARGIN,1);
        /*
         * content : 需要加密的 文字
         * BarcodeFormat.QR_CODE:要解析的类型（二维码）
         * hints：加密涉及的一些参数：编码、排错率
         */
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        //内存中的一张图片：此时需要的图片 是二维码-> 需要一个boolean[][] ->BitMatrix
        //BufferedImage img = MatrixToImageWriter.toBufferedImage(bitMatrix) ;
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                boolean b = bitMatrix.get(i, j);
                int rgb=b?Color.black.getRGB():Color.WHITE.getRGB();
                bufferedImage.setRGB(i, j,rgb);
            }
        }
        //画logo
        bufferedImage = LogoUtil.logoMatrix(bufferedImage, logo) ;
        //String ->File
        File file = new File(imgPath);
        //生成图片
        ImageIO.write(bufferedImage, format,file) ;//format:图片格式
    }
    //解密：二维码->文字
    public static void decodeImg(File file) throws Exception {
        if(!file.exists()) {
            return ;
        }
        //file->内存中的一张图片
        BufferedImage imge = ImageIO.read(file)  ;

        MultiFormatReader formatReader = new MultiFormatReader() ;
        LuminanceSource source = new BufferedImageLuminanceSource(imge);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
        //图片 ->result
        Map map = new HashMap();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8") ;
        Result result = formatReader.decode(binaryBitmap  ,map ) ;
        System.out.println("解析结果："+ result.toString());
    }

    public static void main(String[] args) throws Exception {
        String imgPath = "d.png" ;
        String content = "hello你好" ;
        String logo = "logo.png" ;
        //加密：文字->二维码
        ZXingUtil.encodeImg(imgPath,"gif",content,430,430,logo);

        //解密：二维码->文字
        ZXingUtil.decodeImg(new File(imgPath));
    }
}
